#pragma once
#include "stdafx.h"
#include "BSValue.h"
#include <boost/shared_ptr.hpp>
#include <map>
#include <cctype>
#include <algorithm>

class SchemaManager
{
private:
	
	std::map<std::string , struct Schema*  >schemaMap;
public:
	SchemaManager(void);
	~SchemaManager(void);
	int addSchema(std::string, struct  Schema*);
};

struct Schema
{
	std::string id;
	std::string description;
	std::string type;
	union 
	{
	
		struct
		{
			std::map<std::string, Schema* >* schema; 
		}properties;
		
		struct
		{
			Schema* schema;  
		}items;
	}value;
	Schema(BSValue schemaValue)
	{

		std::string id = schemaValue["id"].asString();
		std::string description = schemaValue["description"].asString();
		std::string type = schemaValue["type"].asString();
		transform(id.begin(), id.end(), id.begin(), ::tolower);;
		transform(description.begin(), description.end(), description.begin(), ::tolower);;
		transform(type.begin(), type.end(), type.begin(), ::tolower);;
		this->id = id;
		this->description = description;
		this->type = type;
		if(this->type=="object")
		{
			
			this->value.properties.schema = new std::map<std::string, Schema* >();
			
			BSValue propertiesValue = schemaValue["properties"];
			BSValue::Members members( propertiesValue.getMemberNames() );  
			for ( BSValue::Members::iterator it = members.begin();  it != members.end();   it++)
			{
				const std::string &name = *it;  
				BSValue value = propertiesValue[name];
				Schema * schema = new Schema(value);
				this->value.properties.schema->insert(make_pair(name,schema));
			}
		}
		else if (this->type=="array")
		{
			BSValue itemsValue = schemaValue["items"];
			Schema * schema = new Schema(itemsValue);
			this->value.items.schema = schema;
		}
	}
    ~Schema()
	{

	}
};